<template>
	<view class="">
		<view class="row j-sb p-3 a-center" @tap="toAddressList()">
			<view class="row flex-column">
				<text><text style="font-weight: bold;">{{name}}</text><text class="font-a ml_20" >{{phone}}</text></text>
				<text>{{address}}</text>
			</view>
			<image src="../../static/rightcir.png" style="width: 35upx;height: 35upx;"></image>
		</view>
		<image src="../../static/line.jpg" style="width: 100%;height: 17upx;"></image>
		<view class="py-1 main-bg-color-hui">
		</view>
		<view class="p-3 j-sb pb-10">
			<view class="font-lg pb-3" style="color: #333333;">谷雨颐嘉健康服务官方产品</view>
			<view class="row j-sb flex-nowrap" >
				<image style="width: 200upx;height: 145upx;background-color: #C8C7CC;" :src="baseUrl+productDetail.img"></image>
				<view class="pl-2 " style="width: 450upx;" >
					<view class="font-md" style="color: #333333;">
						{{productDetail.title}}
					</view>
					<!-- <view class="params">
						<block v-for="(item,index) in productDetail.attrarrs" :key="index">
						|{{item.attrval}}|
						</block>
					</view> -->
					<view class="row j-sb a-center">
						<view class="main-text-color">
							<text class="font-sm">￥</text>
							<text class="font-lg">{{productDetail.price}}</text>
						</view>
						<view class="">
							×{{num}}
						</view>
					</view>
				</view>
			</view>
			
			<view class="row j-start py-3">
				<!-- <text style="color: #333333;">体检包含项目：</text>
				<view class="" style="padding-left: 60upx;">
						<block v-for="(item,index) in productDetail.medical_items" :key="index">
						<view >{{item.medical_items_attrname}}{{item.medical_items_attrval}}</view>
						</block>
				</view> -->
				<block v-for="(items,indexs) in productDetail.medical_items" :key="indexs">
					<view class="checkitem">
						<view class="medical_items ml_20" style="padding: 10rpx 0;">
							<text>{{items.medical_items_attrname}}:</text>
							<text>{{items.medical_items_attrval}}</text>
						</view>
					</view>
				</block>
			</view>
			
			<!-- <view class="px-3 row j-sb a-center" style="height: 133upx;border-top: 1px solid #cccccc;border-bottom: 1px solid #cccccc;">
				<text>数量</text>
				<view class="row text-center" style="height: 58upx;border: 1px solid #cccccc;">
					<view class="" @tap="sum" style="width: 50upx;line-height: 58upx;border-right: 1px solid #cccccc;">
						-
					</view>
					<view class="" style="width: 70upx;line-height: 58upx;">
						{{num}}
					</view>
					<view class="" @tap="add" style="width: 50upx;line-height: 58upx;border-left: 1px solid #cccccc;">
						+
					</view>
				</view>
			</view> -->
			
			<view class="row j-sb a-center" style="height: 108upx;border-bottom: 1px solid #f6f6f6;">
				<text style="color: #333333;">配送方式：</text>
				<text>电子编码，无配送</text>
			</view>
			<!-- <view class="row j-sb a-center" style="height: 108upx;border-bottom: 1px solid #cccccc;">
				<text style="color: #333333;">有效期至：</text>
				<text>2020-10-07</text>
			</view> -->
			<view class="row j-sb a-center" style="height: 108upx;border-bottom: 1px solid #f6f6f6;">
				<text style="color: #333333;">开具发票：</text>
				<view class="row a-center">
					<!-- <image src="../../static/rightcir.png" style="width: 35upx;height: 37upx;"></image> -->
					<switch color="#28a745" style="transform: scale(0.7,0.7)" @change="switchChange" :checked="checked" />
					
					<text>是否开具发票</text>
				</view>
				
			</view>
			<view class="row j-sb a-center" style="height: 108upx;border-bottom: 1px solid #f6f6f6;">
				<text style="color: #333333;">支付方式：</text>
				<view class="row a-center">
					<image src="../../static/nike.jpg" style="width: 35upx;height: 37upx;"></image>
					<text>在线支付</text>
				</view>
				
			</view>
			<view class="row flex-column j-sa" style="height: 168upx;border-bottom: 1px solid #f6f6f6;">
				<view class="row j-sb a-center">
					<text style="color: #333333;">商品金额：</text>
					<text>￥{{money}}</text>
				</view>
				<view class="row j-sb a-center">
					<text style="color: #333333;">积分</text>
					<text>您有{{score}}积分，使用 
						<input type="number" @input="inputScore" :value="scoreNum" style="display: inline-block;vertical-align: middle;border: 1px solid #f6f6f6;width: 90upx;height: 47upx;font-size:24upx;text-align: center;;" /> 积分
						<text style="color: #333333;">  -￥{{scoreMoney}}</text>
					</text>
				</view>
			</view>
		</view>
		
		<view class="position-fixed bottom-0 row text-center main-bg-color-bai" style="height: 95upx;width: 750upx;">
			<view style="line-height: 95upx;width:66%;">
				共{{num}}件，应付：<text class="main-text-color">￥<text class="font-lg font-weight-6">{{moneys}}</text></text>
			</view>
			<view @tap="orderInfo()" class="main-bg-color-cheng main-text-color-bai " style="line-height: 95upx;width:34%;">
				提交订单
			</view>
		</view>
		
	</view>
</template>

<script>
	import request from '@/common/lib/request.js'
	import jweixin from '@/common/lib/jweixin.js'//1.6.0
	export default {
		data() {
			return {
				baseUrl:request.common.baseUrl,
				product_id:0,
				num:0,
				money:0,
				score:0,
				addressId:0,
				area_code:'',
				address:'',
				name:'',
				phone:'',
				productDetail:'',
				checked:false,
				is_invoice:1,
				scoreMoney:0,
				scoreData:'',
				moneys:0,
				scoreNum:0,
				params:''
				
			}
		},
		onLoad: function(option){
			this.product_id=option.product_id
			this.num=option.buynums
			uni.request({
				url: request.common.baseUrl+'/api/address/getDefaultAddress',
				data: {
					token:uni.getStorageSync('keyToken')
				},
				header: {
					'Content-Type':'application/json;charset=UTF-8',
					'Content-Type':'application/x-www-form-urlencoded'
				},
				success: (res) => {
					if(res.data.code==200){
						this.addressId=res.data.data.id
						this.area_code=res.data.data.area_code
						this.address=res.data.data.province+res.data.data.city
						if(res.data.data.district){
							this.address=this.address+res.data.data.district
						}
						this.address=this.address+res.data.data.uaddress
						this.name=res.data.data.uname
						this.phone=res.data.data.uphone
					}else{
						uni.request({
							url: request.common.baseUrl+'/api/address/isAddress',
							data: {
								token:uni.getStorageSync('keyToken')
							},
							header: {
								'Content-Type':'application/json;charset=UTF-8',
								'Content-Type':'application/x-www-form-urlencoded'
							},
							success: (res) => {
								if(res.data.code==200){
									uni.navigateTo({
										url: '/pages/score/address'
									});
								}else{
									uni.navigateTo({
										// url: '/pages/member/member'
										url: '/pages/score/address'
									});
								}
							}
						})
					}
				}
			});
			uni.request({
				url: request.common.baseUrl+'/api/index/productDetail',
				data: {
					id:option.product_id
				},
				header: {
					'Content-Type':'application/json;charset=UTF-8',
					'Content-Type':'application/x-www-form-urlencoded'
				},
				success: (res) => {
					if(res.data.code==200){
						this.productDetail=res.data.data
						this.money=this.num*this.productDetail.price
						this.money=this.money.toFixed(2)
						this.moneys=this.money-this.scoreMoney
						for (var i=0;i<this.productDetail.attrarrs.length;i++) {
							if(i==0){
								this.params=this.params+this.productDetail.attrarrs[i].attrval
							}else{
								this.params=this.params+','+this.productDetail.attrarrs[i].attrval
							}
						}
					}
				}
			});
			uni.request({
				url: request.common.baseUrl+'/api/distributor/myScore',
				data: {
					token:uni.getStorageSync('keyToken')
				},
				header: {
					'Content-Type':'application/json;charset=UTF-8',
					'Content-Type':'application/x-www-form-urlencoded'
				},
				success: (res) => {
					if(res.data.code==200){
						this.score=res.data.data.score
					}
				}
			});
			// uni.request({
			// 	url: request.common.baseUrl+'/api/index/systemSet',
			// 	data: {},
			// 	header: {
			// 		'Content-Type':'application/json;charset=UTF-8',
			// 		'Content-Type':'application/x-www-form-urlencoded'
			// 	},
			// 	success: (res) => {
			// 		if(res.data.code==200){
			// 			this.scoreData=res.data.data
			// 		}
			// 	}
			// });
			
			
		},
		onShow:function(){
			uni.request({
				url: request.common.baseUrl+'/api/address/getDefaultAddress',
				data: {
					token:uni.getStorageSync('keyToken')
				},
				header: {
					'Content-Type':'application/json;charset=UTF-8',
					'Content-Type':'application/x-www-form-urlencoded'
				},
				success: (res) => {
					if(res.data.code==200){
						this.addressId=res.data.data.id
						this.area_code=res.data.data.area_code
						this.address=res.data.data.province+res.data.data.city
						if(res.data.data.district){
							this.address=this.address+res.data.data.district
						}
						this.address=this.address+res.data.data.uaddress
						this.name=res.data.data.uname
						this.phone=res.data.data.uphone
					}else{
						uni.request({
							url: request.common.baseUrl+'/api/address/isAddress',
							data: {
								token:uni.getStorageSync('keyToken')
							},
							header: {
								'Content-Type':'application/json;charset=UTF-8',
								'Content-Type':'application/x-www-form-urlencoded'
							},
							success: (res) => {
								if(res.data.code==200){
									uni.navigateTo({
										url: '/pages/score/address'
									});
								}else{
									uni.navigateTo({
										// url: '/pages/member/member'
										url: '/pages/score/address'
									});
								}
							}
						})
					}
				}
			});
		},
		methods: {
			sum(){
				if(this.num>1){
					this.num--;
					this.money=this.num*this.productDetail.price
					this.money=this.money.toFixed(2)
					this.moneys=this.money-this.scoreMoney
				}
			},
			add(){
				this.num++;
				this.money=this.num*this.productDetail.price
				this.money=this.money.toFixed(2)
				this.moneys=this.money-this.scoreMoney
			},
			toAddressList(){
				uni.navigateTo({
					url: '/pages/score/address'
				});
			},
			switchChange: function (e) {
				if(e.target.value==true){
					this.is_invoice=2
				}else{
					this.is_invoice=1
				}
			},
			inputScore: function (e) {
				if(!/^[0-9]*$/.test(e.detail.value)){
					uni.showModal({
					    title: '提示',
					    content: '请输入合适的数字',
					    success: function (res) {
					        if (res.confirm) {
					        } else if (res.cancel) {
					        }
					    }
					});
				}
				if(e.detail.value>this.score){
					uni.showToast({
						title:'不能高于'+this.score,
						icon: 'none',
					})
				}
				// this.scoreMoney=e.detail.value/this.scoreData.buy_score
				if(e.detail.value){
					this.scoreMoney=parseFloat(e.detail.value)
					this.scoreNum=this.scoreMoney
					this.scoreMoney=this.scoreMoney.toFixed(2)
					this.moneys=this.money-this.scoreMoney
					if(this.moneys<0){
						uni.showModal({
						    title: '提示',
						    content: '请输入合适的数字',
						    success: function (res) {
						        if (res.confirm) {
						        } else if (res.cancel) {
						        }
						    }
						});
					}
				}else{
					this.scoreMoney=parseFloat(0)
					this.scoreNum=this.scoreMoney
					this.scoreMoney=this.scoreMoney.toFixed(2)
					this.moneys=this.money-this.scoreMoney
					if(this.moneys<0){
						uni.showModal({
						    title: '提示',
						    content: '请输入合适的数字',
						    success: function (res) {
						        if (res.confirm) {
						        } else if (res.cancel) {
						        }
						    }
						});
					}
				}
				
			},
			orderInfo(){
				var that=this;
				uni.request({
				    url: request.common.baseUrl+'/api/order/worderIn',
				    data: {
						token:uni.getStorageSync('keyToken'),
						openid:uni.getStorageSync('keyOpenid'),
						product_id:this.product_id,
						// store_id:uni.getStorageSync('keyStoreId'),
						pay_number:this.num,
						price:this.productDetail.price,
						pay_fee:this.moneys,
						pay_score:this.scoreNum,
						uname:this.name,
						uphone:this.phone,
						uaddress:this.address,
						area_code:this.area_code,
						params:this.params,
						pay_style:2,//1支付宝；2微信 
						address_id:this.addressId,
						title:this.productDetail.title,
						is_invoice:this.is_invoice
					},
				    header: {
						'Content-Type':'application/json;charset=UTF-8',
						'Content-Type':'application/x-www-form-urlencoded'
				    },
				    success: (res) => {
				        if(res.data.code==200){
							jweixin.chooseWXPay({
								nonceStr: res.data.data.nonceStr,
								timestamp: res.data.data.timeStamp,
								package: res.data.data.package,
								signType: res.data.data.signType,
								paySign: res.data.data.paySign,
								success: () => {
									uni.navigateTo({
										url:'/pages/order/paySuccess?pay_fee='+that.moneys
									})
								},
								fail: err => {
									// 支付失败
								},
								cancel: err => {
									// 支付取消
								}
							});
						}else{
							uni.showToast({
								title:res.data.msg,
								icon: 'none',
							})
						}
				    }
				});
			}
		}
	}
</script>

<style>
	.ml_10 {
		margin-left: 10rpx;
	}
	.ml_20 {
		margin-left: 20rpx;
	}
	.row_pay {
		display: flex;
		align-items: center;
	}
	.font-a {
		font-family: Arial, Helvetica, sans-serif;
	}
	.checkitem {
		width: 100%;
		border-bottom: 1px dashed #e3e3e3;
	}
</style>
